package com.jnu.tijian.service;

import com.jnu.tijian.domain.Users;
import com.jnu.tijian.dto.ResponseObject;
import com.jnu.tijian.exception.TijianAPPException;
import com.jnu.tijian.exception.TijianAPPExceptionEnum;
import com.jnu.tijian.mapper.UsersMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class UsersServiceImpl implements UsersService {
    @Autowired
    UsersMapper usersMapper;

    /**
     * 处理登录请求
     */
//    @Override
//    public ResponseObject<Users> login(Users user) {
//        ResponseObject<Users> responseObject = null;
//
//        // 1. 查询
//        Users result = usersMapper.selectByPrimaryKey(user.getUserId());
//
//        // 2. 是否为空
//        if (result == null) {
//            // 告诉前端
//            responseObject = new ResponseObject<>(201, "该手机号尚未注册！！！");
//            return responseObject;
//        }
//
//        // 3. 比较密码
//        if(!result.getPassword().equals(user.getPassword())) {
//            // 告诉前端
//            responseObject = new ResponseObject<>(202, "密码错误！！！");
//            return responseObject;
//        }
//
//        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, result);
//
//        return responseObject;
//    }

    @Override
    public ResponseObject<Users> login(Users user) {
        ResponseObject<Users> responseObject = null;

        // 1. 查询
        Users result = usersMapper.selectByPrimaryKey(user.getUserId());

        // 2. 是否为空
        if (result == null) {
            // 告诉前端
            // responseObject = new ResponseObject<>(201, "该手机号尚未注册！！！");
            throw new TijianAPPException(TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getCode(), TijianAPPExceptionEnum.MOBILE_NOT_REGISTER.getMessage());
        }

        // 3. 比较密码
        if(!result.getPassword().equals(user.getPassword())) {
            // 告诉前端
            // responseObject = new ResponseObject<>(202, "密码错误！！！");
            throw new TijianAPPException(TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getCode(),  TijianAPPExceptionEnum.USERID_OR_PASSWORD_IS_WRONG.getMessage());
        }

        result.setPassword(""); // 密码置空 不能返回给前端
        responseObject = new ResponseObject<>(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC, result);

        return responseObject;
    }

    @Override
    public ResponseObject register(Users users) {
        // 1.先查询是否有重复手机号 如果有则抛出重复注册异常
        if(usersMapper.selectByPrimaryKey(users.getUserId())!=null){
            throw new TijianAPPException(TijianAPPExceptionEnum.USERID_HAS_REGISTERED.getCode(), TijianAPPExceptionEnum.USERID_HAS_REGISTERED.getMessage());
        }

        // 2.没有重复手机号 则往数据库插入一条新的用户信息
        usersMapper.insertSelective(users);

        // 3.成功注册
        System.out.println("注册成功！手机号："+users.getUserId());
        return new ResponseObject(ResponseObject.SUCCESS_STATUS, ResponseObject.SUCCESS_DESC);
    }
}
